unit uLib;

interface

function LibOpen(libName: String): Cardinal;
function LibProc(lib: Cardinal; procName: String): Pointer;
procedure LibClose(lib: Cardinal);

implementation

uses
{$IFDEF WIN32}
	Windows,
{$ENDIF}
  uTyp;

{$IFNDEF WIN32}
function dlopen(name: PChar; flags: Longint): Pointer; cdecl; external 'dl';
function dlsym(lib: Pointer; name: PChar): Pointer; cdecl; external 'dl';
function dlclose(lib: Pointer): Longint; cdecl; external 'dl';
{$ENDIF}

function LibOpen(libName: String): Cardinal;
begin
{$IFDEF WIN32}
	Result := LoadLibrary(PChar(MakePath(libName)));
{$ELSE}
	Result := Cardinal(dlopen(PChar(libName), $001));

  if Result = 0 then Result := Cardinal(dlopen(PChar(libName + '.0'), $001));
  if Result = 0 then Result := Cardinal(dlopen(PChar(libName + '.1'), $001));
{$ENDIF}
end;

function LibProc(lib: Cardinal; procName: String): Pointer;
begin
{$IFDEF WIN32}
	Result := GetProcAddress(lib, PChar(procName));
{$ELSE}
	Result := dlsym(Pointer(lib), PChar(procName));
{$ENDIF}
end;

procedure LibClose(lib: Cardinal);
begin
{$IFDEF WIN32}
	if lib <> 0 then FreeLibrary(lib);
{$ELSE}
	if lib <> 0 then dlclose(Pointer(lib));
{$ENDIF}
end;

end.
